package top.infopub.mgr.common.tags;


import java.util.Iterator;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

import top.infopub.model.sys.ParameterGroupDetail;
import top.infopub.tdp.util.GrpParaUtil;


/**
 * 此标签为select标签 </br>
 * 使用例子：</br>
 * <infopub:select name="gcarea" value="1" list="{1:中国,2:韩国,3:英国,4:美国}" type="list"/></br>
 * <infopub:select name="navitype" value="0" type="dic" groupNo="A0001"/></br>
 * @author Awoke
 * @date 2018年10月15日
 * 
 */
public class SelectTag extends TagSupport {

    private static final long serialVersionUID = 1367677657034147853L;

    private String id;

    /*数据字典组编号*/
    private String groupNo;

    private String type;

    private String list;/* {1:选项1,2:选项2} */

    private String name;

    private String value;

    private String cssClass;

    private String cssStyle;

    private String reg;

    private String tip;

    private String exp;

    private String entire;

    private String disabled;

    /** 请选择后的：xx **/
    private String entireName = "";

    /** 附加属性 easyui-combobox，如 data-options="required:true"  **/
    private String dataOptions;

    /**
     * layui 是否开启验证
     */
    private String layVerify;

    /**
     * layui 增加监听事件的过滤器
     */
    private String layFilter;

    /**
     * layui 是否开启搜索匹配功能 true则开启
     */
    private String laySearch;

    @Override
    public int doStartTag()
        throws JspException {

        StringBuilder sbr = new StringBuilder();
        if (StringUtils.isBlank(id)) {
            id = name;
        }
        sbr.append("<select id=\"" + id + "\" name=\"" + name + "\" ");

        if (StringUtils.isNotBlank(cssClass)) {
            sbr.append(" class=\"" + cssClass + "\" ");
        }

        if (StringUtils.isNotBlank(cssStyle)) {
            sbr.append(" style=\"" + cssStyle + "\" ");
        }

        if (StringUtils.isNotBlank(dataOptions)) {
            sbr.append(" data-options=\"" + dataOptions + "\" ");
        }

        if (StringUtils.isNotBlank(layVerify)) {
            sbr.append(" lay-verify=\"" + layVerify + "\" ");
        }

        if (StringUtils.isNotBlank(layFilter)) {
            sbr.append(" lay-filter=\"" + layFilter + "\" ");
        }
        
        if (StringUtils.equalsIgnoreCase("true", laySearch)) {
            sbr.append(" lay-search ");
        }

        if (StringUtils.isNotBlank(reg)) {
            sbr.append(" reg=\"" + reg + "\" ");
        }

        if (StringUtils.isNotBlank(exp)) {
            sbr.append(" exp=\"" + exp + "\"");
        }

        if (StringUtils.isNotBlank(tip)) {
            sbr.append(" tip=\"" + tip + "\" ");
        }

        if (StringUtils.isNotBlank(disabled)) {
            sbr.append(" disabled=\"" + disabled + "s\" ");
        }

        sbr.append(">\n");
        if (StringUtils.equalsIgnoreCase(entire, "true") || StringUtils.isNotBlank(entireName)) {
            sbr.append("\t\t<option value=\"\">请选择" + entireName + "</option>\n");
        }

        try {
            String _v = value;
            if (type.equalsIgnoreCase("dic") && StringUtils.isNotBlank(groupNo)) {
                Set<ParameterGroupDetail> parameterGroupDetailList = GrpParaUtil.get(groupNo);
                Iterator<ParameterGroupDetail> iterator = parameterGroupDetailList.iterator();
                while (iterator.hasNext()) {
                    ParameterGroupDetail parameterGroupDetail = iterator.next();
                    if (null != parameterGroupDetail) {
                        String isSelect = "";
                        if (StringUtils.isNotBlank(_v) && parameterGroupDetail.getSvalue() != null
                            && _v.equals(parameterGroupDetail.getSvalue())) {

                            isSelect = "selected";
                        }
                        sbr.append("\t\t<option value=\""
                                   + String.valueOf(parameterGroupDetail.getSvalue()) + "\" "
                                   + isSelect + ">" + parameterGroupDetail.getSname()
                                   + "</option>\n");
                    }
                }
            }

            if (type.equals("list") && StringUtils.isNotBlank(list)) {
                String _list = list.substring(1, list.length() - 1);// 去掉前后花括号
                String[] _listArr = _list.split(",");
                for (int j = 0; j < _listArr.length; j++ ) {
                    String[] _listTemp = _listArr[j].split(":");
                    String isSelect = "";
                    if (StringUtils.isNotBlank(_v) && StringUtils.isNotBlank(_listTemp[0])
                        && _v.equals(_listTemp[0])) {

                        isSelect = "selected";
                    }
                    sbr.append("\t\t<option value=\"" + _listTemp[0] + "\" " + isSelect + ">"
                               + _listTemp[1] + " </option>\n");
                }
            }
            sbr.append("\t</select>\n");
            pageContext.getOut().write(sbr.toString());
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return super.doStartTag();
    }

    @Override
    public void release() {
        super.release();
        id = "";
        type = "";
        list = "";
        name = "";
        value = "";
        cssClass = "";
        cssStyle = "";
        reg = "";
        tip = "";
        exp = "";
        entire = "";
        entireName = "";
        disabled = "";
        dataOptions = "";
        layVerify = "";
        layFilter = "";
        laySearch = "";
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getList() {
        return list;
    }

    public void setList(String list) {
        this.list = list;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getCssClass() {
        return cssClass;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

    public String getCssStyle() {
        return cssStyle;
    }

    public void setCssStyle(String cssStyle) {
        this.cssStyle = cssStyle;
    }

    public String getReg() {
        return reg;
    }

    public void setReg(String reg) {
        this.reg = reg;
    }

    public String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }

    public String getExp() {
        return exp;
    }

    public void setExp(String exp) {
        this.exp = exp;
    }

    public String getGroupNo() {
        return groupNo;
    }

    public void setGroupNo(String groupNo) {
        this.groupNo = groupNo;
    }

    public String getEntire() {
        return entire;
    }

    public void setEntire(String entire) {
        this.entire = entire;
    }

    public String getEntireName() {
        return entireName;
    }

    public void setEntireName(String entireName) {
        this.entireName = entireName;
    }

    public String getDisabled() {
        return disabled;
    }

    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }

    public String getDataOptions() {
        return dataOptions;
    }

    public void setDataOptions(String dataOptions) {
        this.dataOptions = dataOptions;
    }

    public String getLayVerify() {
        return layVerify;
    }

    public void setLayVerify(String layVerify) {
        this.layVerify = layVerify;
    }

    public String getLayFilter() {
        return layFilter;
    }

    public void setLayFilter(String layFilter) {
        this.layFilter = layFilter;
    }

    public String getLaySearch() {
        return laySearch;
    }

    public void setLaySearch(String laySearch) {
        this.laySearch = laySearch;
    }

}